{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Difference by Remapping a Digit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minMaxDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #替换一个数字后的最大差值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数&nbsp;<code>num</code>&nbsp;。你知道 Danny Mittal 会偷偷将 <code>0</code>&nbsp;到 <code>9</code>&nbsp;中的一个数字 <strong>替换</strong> 成另一个数字。</p>\n",
    "\n",
    "<p>请你返回将 <code>num</code>&nbsp;中&nbsp;<strong>恰好一个</strong>&nbsp;数字进行替换后，得到的最大值和最小值的差为多少。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>当 Danny 将一个数字 <code>d1</code> 替换成另一个数字 <code>d2</code> 时，Danny 需要将&nbsp;<code>nums</code>&nbsp;中所有 <code>d1</code>&nbsp;都替换成&nbsp;<code>d2</code>&nbsp;。</li>\n",
    "\t<li>Danny 可以将一个数字替换成它自己，也就是说&nbsp;<code>num</code>&nbsp;可以不变。</li>\n",
    "\t<li>Danny 可以将数字分别替换成两个不同的数字分别得到最大值和最小值。</li>\n",
    "\t<li>替换后得到的数字可以包含前导 0 。</li>\n",
    "\t<li>Danny Mittal 获得周赛 326 前 10 名，让我们恭喜他。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>num = 11891\n",
    "<b>输出：</b>99009\n",
    "<b>解释：</b>\n",
    "为了得到最大值，我们将数字 1 替换成数字 9 ，得到 99899 。\n",
    "为了得到最小值，我们将数字 1 替换成数字 0 ，得到 890 。\n",
    "两个数字的差值为 99009 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>num = 90\n",
    "<b>输出：</b>99\n",
    "<strong>解释：</strong>\n",
    "可以得到的最大值是 99（将 0 替换成 9），最小值是 0（将 9 替换成 0）。\n",
    "所以我们得到 99 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num &lt;= 10<sup>8</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-difference-by-remapping-a-digit](https://leetcode.cn/problems/maximum-difference-by-remapping-a-digit/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-difference-by-remapping-a-digit](https://leetcode.cn/problems/maximum-difference-by-remapping-a-digit/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['11891', '90']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        \n",
    "        s = str(num)\n",
    "        amin = int(s.replace(s[0],'0'))\n",
    "        amax = num\n",
    "        for a in s:\n",
    "            if a < '9':\n",
    "                amax = int(s.replace(a,'9'))\n",
    "                break\n",
    "        print(amax,amin)\n",
    "        return amax - amin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        # 29000\n",
    "        # 123456789\n",
    "        for x in str(num):\n",
    "            if x != '9':\n",
    "                break\n",
    "        else:\n",
    "            t_min = 0\n",
    "\n",
    "        t_max = int(str(num).replace(x, '9'))\n",
    "        for c in str(num):\n",
    "            if c != '0':\n",
    "                break\n",
    "        else:\n",
    "            t_max = 0\n",
    "\n",
    "        t_min = int(str(num).replace(c, '0'))\n",
    "\n",
    "        return t_max-t_min\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        str_num = str(num)\n",
    "        mini= int(str_num.replace(str_num[0],'0'))\n",
    "        for i in range(len(str_num)):\n",
    "            if str_num[i] != '9':\n",
    "                return int(str_num.replace(str_num[i], '9')) - mini\n",
    "        return num\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        str_num = str(num)\n",
    "        min_str = int(str_num.replace(str_num[0], '0'))\n",
    "        max_num = num\n",
    "        for i in str_num:\n",
    "            if i != '9':\n",
    "                max_num = int(str_num.replace(i, '9'))\n",
    "                break\n",
    "\n",
    "        return max_num - min_str\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        for x in str(num):\n",
    "            if x != '9':\n",
    "                break\n",
    "        else:\n",
    "            t_min = 0\n",
    "\n",
    "        t_max = int(str(num).replace(x, '9'))\n",
    "        for c in str(num):\n",
    "            if c != '0':\n",
    "                break\n",
    "        else:\n",
    "            t_max = 0\n",
    "\n",
    "        t_min = int(str(num).replace(c, '0'))\n",
    "        return t_max - t_min\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        index1 = '10'\n",
    "        for i in range(len(s)):\n",
    "            if s[i] != '9':\n",
    "                index1 = s[i]\n",
    "                break\n",
    "        index2 = s[0]\n",
    "        min_,max_ = '',''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == index1:\n",
    "                max_ += '9'\n",
    "            else:\n",
    "                max_ += s[i]\n",
    "            if s[i] == index2:\n",
    "                min_ += '0'\n",
    "            else:\n",
    "                min_ += s[i]\n",
    "        return int(max_) - int(min_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        max_num,min_num = str(num),str(num)\n",
    "        for i in max_num:\n",
    "            if i != '9':\n",
    "                max_num = max_num.replace(i,'9')\n",
    "                break\n",
    "        min_num = min_num.replace(min_num[0],'0')\n",
    "        return int(max_num) - int(min_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx=num\n",
    "        s=str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c,'9'))\n",
    "                break\n",
    "        \n",
    "\n",
    "        return mx - int(s.replace(s[0],'0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        index = 0\n",
    "        for i in range(len(num)):\n",
    "            if num[i] != \"9\":\n",
    "                index = i\n",
    "                break\n",
    "        maxNum = int(num.replace(num[i], \"9\"))\n",
    "        minNum = int(num.replace(num[0], \"0\"))\n",
    "        print(maxNum)\n",
    "        print(minNum)\n",
    "        return maxNum - minNum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        k_shouwei = str(num)[0]\n",
    "        min_number = ''\n",
    "        max_number = ''\n",
    "        for i in str(num):\n",
    "            if i == k_shouwei:\n",
    "                min_number += '0'\n",
    "            else:\n",
    "                min_number += i\n",
    "        for j in str(num):\n",
    "            if j!='9':\n",
    "                k = j\n",
    "                break\n",
    "            else:\n",
    "                k='9'\n",
    "        for j in str(num):\n",
    "            if j==k:\n",
    "                max_number += '9'\n",
    "            else:\n",
    "                max_number += j\n",
    "        return int(max_number)-int(min_number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        nums1 = str(num)\n",
    "        n = len(nums1)\n",
    "\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if nums1[i] != '9':\n",
    "                nums1 = nums1.replace(nums1[i],'9')\n",
    "                break\n",
    "            else:\n",
    "                i += 1\n",
    "        nums2 = str(num)\n",
    "        nums2 = nums2.replace(nums2[0],'0')\n",
    "\n",
    "        return int(nums1)-int(nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        n=str(num)\n",
    "        s=n[0]\n",
    "        b=n.replace(s,'0')\n",
    "        for i in n:\n",
    "            if i!='9':\n",
    "                a=n.replace(i,'9')\n",
    "                break\n",
    "        else:\n",
    "            a=num\n",
    "        return int(a)-int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num =str(num)\n",
    "        small = int(num.replace(num[0], '0'))\n",
    "        num1 = num.lstrip('9')\n",
    "        big = int(num.replace(num1[0], '9') if num1 else num)\n",
    "        return big - small\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num_str=str(num)\n",
    "        max_list=[]\n",
    "        min_list=[]\n",
    "        for x in \"0123456789\":\n",
    "            max_list.append(num_str.replace(f\"{x}\",\"9\"))\n",
    "        for x in \"0123456789\":\n",
    "            min_list.append(num_str.replace(f\"{x}\",\"0\"))\n",
    "        max_value=max(map(int,max_list))\n",
    "        min_value=min(map(int,min_list))\n",
    "        return max_value-min_value\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num=str(num)\n",
    "        num1=num\n",
    "        a=len(num)\n",
    "        for i in range(a):\n",
    "            if num[i]!='9':\n",
    "                tem=num[i]\n",
    "                num=num.replace(num[i],'9')\n",
    "                print(num)\n",
    "                break\n",
    "        print(num)\n",
    "        num=int(num)\n",
    "        tem=num1[0]\n",
    "        num1=num1.replace(num1[0],'0')\n",
    "        num1=int(num1)\n",
    "        print(num1)\n",
    "        return num-num1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        min_value = num\n",
    "        max_value = num\n",
    "        for ind, i in enumerate(str(num)):\n",
    "            if i !=\"0\" and min_value ==num:\n",
    "                min_value = int(str(num).replace(str(num)[ind], \"0\"))\n",
    "            if i !=\"9\" and max_value ==num:\n",
    "                max_value = int(str(num).replace(str(num)[ind], \"9\" ))\n",
    "        return max_value-min_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        for i in str(num):\n",
    "            if i != \"9\":\n",
    "                a = str(num).replace(i,\"9\")\n",
    "                # print(a)\n",
    "                break\n",
    "            else:a = num\n",
    "        for i in str(num):\n",
    "            if i != \"0\":\n",
    "                b = str(num).replace(i,\"0\")\n",
    "                break\n",
    "            else:\n",
    "                b = num\n",
    "        # print(int(a)-int(b))\n",
    "        return (int(a)-int(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        a = num\n",
    "        num = str(num)\n",
    "        n = len(num)\n",
    "        for i in range(n):\n",
    "            if num[i] != '9':\n",
    "                # f = num[i]\n",
    "                a = num.replace(num[i], '9')\n",
    "                break\n",
    "        # b = num.replace(num[0], '0')\n",
    "        # print(a, b, int(a)-int(b))\n",
    "        return int(a)-int(num.replace(num[0], '0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s,maxn=str(num),num\n",
    "        for i in s:\n",
    "            if i!='9':\n",
    "                maxn=int(s.replace(i,'9'))\n",
    "                break\n",
    "        return maxn-int(s.replace(s[0],'0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        l = ''\n",
    "        for i in s:\n",
    "            if i != '9':\n",
    "                l = i\n",
    "                break\n",
    "        mx = num\n",
    "        if l:\n",
    "            mx = int(s.replace(l, '9'))\n",
    "        mn = int(s.replace(s[0], '0'))\n",
    "        return mx - mn\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        f = '9'\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                f = c \n",
    "                break\n",
    "        t = s[0]\n",
    "        return int(s.replace(f, '9')) - int(s.replace(t, '0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        flag = s[0]\n",
    "        first = s[0]\n",
    "        for k in s:\n",
    "            if k!='9':\n",
    "                first = k\n",
    "                break\n",
    "        big,small = \"\",\"\"\n",
    "        for i in s:\n",
    "            if i==first:\n",
    "                big += \"9\"\n",
    "            else:\n",
    "                big += i\n",
    "            if i==flag:\n",
    "                small += '0'\n",
    "            else:\n",
    "                small += i\n",
    "        return int(big)-int(small)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        nums = str(num)\n",
    "        ma = num\n",
    "        mi = int(nums.replace(nums[0], \"0\"))\n",
    "\n",
    "        for s in nums:\n",
    "            if s != \"9\":\n",
    "                ma = int(nums.replace(s, \"9\"))\n",
    "                break\n",
    "\n",
    "        return ma  - mi\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num=str(num)\n",
    "        num1=num\n",
    "        for i in num:\n",
    "            if i!='9':\n",
    "                tem=i\n",
    "                num=num.replace(i,'9')\n",
    "                break\n",
    "        num1=num1.replace(num1[0],'0')\n",
    "        num=int(num)\n",
    "        num1=int(num1)\n",
    "        return num-num1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        x = []\n",
    "        x1 = []\n",
    "        x2 = []\n",
    "        for i in list(str(num)):\n",
    "           i = int(i)\n",
    "           x.append(i)\n",
    "           x1.append(i)\n",
    "           x2.append(i)\n",
    "    \n",
    "        repalcemax,repalcemin = -1,-1\n",
    "        for i in range(0, len(x)):\n",
    "            if x[i] != 0:\n",
    "                repalcemin = i\n",
    "                break\n",
    "        \n",
    "        for i in range(0,len(x)):\n",
    "            if x[i]!= 9:\n",
    "                repalcemax = i\n",
    "                break\n",
    "\n",
    "        for i in range(0, len(x1)):\n",
    "            if x1[i] == x[repalcemax] and repalcemax != -1 :\n",
    "                x1[i] = 9\n",
    "    \n",
    "        for j in range(0, len(x2)):\n",
    "            if x2[j] == x[repalcemin]:\n",
    "                x2[j] = 0\n",
    "        maxnum = int(str(x1).replace(\"[\",\"\").replace(\"]\",\"\").replace(\",\",\"\").replace(\" \",\"\"))\n",
    "        minnum = int(str(x2).replace(\"[\",\"\").replace(\"]\",\"\").replace(\",\",\"\").replace(\" \",\"\"))\n",
    "\n",
    "        return maxnum-minnum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        cha = str(num)\n",
    "        fst = cha[0]\n",
    "        cmx = num\n",
    "\n",
    "        for cha_curr in cha:\n",
    "            if cha_curr != '9':\n",
    "                cmx = int(cha.replace(cha_curr, '9'))\n",
    "                break\n",
    "        \n",
    "        cmi = int(cha.replace(fst, '0'))\n",
    "        #print(cmx, cmi)\n",
    "        return cmx - cmi\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        n = next((c for c in num if c != '9'), '9')\n",
    "        mx = int(num.replace(n, '9'))\n",
    "        mn = int(num.replace(num[0], '0'))\n",
    "        return mx - mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        mx = num\n",
    "        for i in s:\n",
    "            if i != '9':\n",
    "                mx = int(s.replace(i, '9'))\n",
    "                break\n",
    "        mn = int(s.replace(s[0], '0'))\n",
    "        return mx - mn\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        min_val = list(str(num))\n",
    "        first = None\n",
    "        for i, s in enumerate(min_val):\n",
    "            if i==0:\n",
    "                first = s\n",
    "                min_val[i] = '0'\n",
    "            elif s==first:\n",
    "                min_val[i] = '0'\n",
    "        min_val = int(''.join(min_val))\n",
    "\n",
    "        max_val = list(str(num))\n",
    "        first = None      \n",
    "        \n",
    "        i = 0\n",
    "        while i<len(max_val) and max_val[i]=='9':\n",
    "            i += 1\n",
    "        \n",
    "        for j in range(i, len(max_val)):\n",
    "            if j==i:\n",
    "                first = max_val[j]\n",
    "                max_val[j] = '9'\n",
    "            elif max_val[j]==first:\n",
    "                max_val[j] = '9'\n",
    "\n",
    "\n",
    "        return int(''.join(max_val))-min_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        min_num = s.replace(s[0],'0')\n",
    "        max_num = s\n",
    "        for c in s:\n",
    "            if c!='9':\n",
    "                max_num = s.replace(c,'9')\n",
    "                break\n",
    "        return int(max_num)-int(min_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        digits = []\n",
    "        t = num\n",
    "        while t:\n",
    "            digits.append(t % 10)\n",
    "            t //= 10\n",
    "        digits.reverse()\n",
    "        max_change_digit = -1\n",
    "        min_change_digit = -1\n",
    "        max_val = 0\n",
    "        min_val = 0\n",
    "        for d in digits:\n",
    "            if d < 9:\n",
    "                max_change_digit = d\n",
    "                break\n",
    "        for d in digits:\n",
    "            if d > 0:\n",
    "                min_change_digit = d\n",
    "                break\n",
    "        if max_change_digit == -1:\n",
    "            max_val = num\n",
    "        else:\n",
    "            t = num\n",
    "            i = 1\n",
    "            while t:\n",
    "                max_val += (t % 10 if t % 10 != max_change_digit else 9) * i\n",
    "                i *= 10\n",
    "                t //= 10\n",
    "        if min_change_digit == -1:\n",
    "            min_val = 0\n",
    "        else:\n",
    "            t = num\n",
    "            i = 1\n",
    "            while t:\n",
    "                min_val += (t % 10 if t % 10 != min_change_digit else 0) * i\n",
    "                i *= 10\n",
    "                t //=10\n",
    "        return max_val - min_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "\n",
    "        s = str(num)\n",
    "\n",
    "        for c in s:\n",
    "\n",
    "            if c != '9':\n",
    "\n",
    "                mx = int(s.replace(c, '9'))\n",
    "\n",
    "                break\n",
    "\n",
    "        return mx - int(s.replace(s[0], '0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        min_=0\n",
    "        for i in range(len(s)):\n",
    "            cur=int(s[i]) if s[i]!=s[0] else 0\n",
    "            min_=10*min_+cur\n",
    "        j=0\n",
    "        while j<len(s) and s[j]=='9':\n",
    "            j +=1\n",
    "        if j==len(s): \n",
    "            return num-min_\n",
    "        max_=0\n",
    "        for i in range(len(s)):\n",
    "            cur=int(s[i]) if s[i]!=s[j] else 9\n",
    "            max_=10*max_+cur\n",
    "        return max_-min_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        index = 0\n",
    "        for i in range(len(num)):\n",
    "            if num[i] != \"9\":\n",
    "                index = i\n",
    "                break\n",
    "        maxNum = int(num.replace(num[i], \"9\"))\n",
    "        minNum = int(num.replace(num[0], \"0\"))\n",
    "        return maxNum - minNum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        stack = []\n",
    "        while num:\n",
    "            stack.append(num % 10)\n",
    "            num //= 10\n",
    "        \n",
    "        flag = stack[-1]\n",
    "        minVal = 0\n",
    "        for i in range(len(stack)-1, -1, -1):\n",
    "            if stack[i] == flag:\n",
    "                minVal = minVal * 10\n",
    "            else:\n",
    "                minVal = minVal * 10 + stack[i]\n",
    "                \n",
    "        i = len(stack)-1\n",
    "        maxVal = 0\n",
    "        while i >= 0 and stack[i] == 9:\n",
    "            maxVal = maxVal * 10 + 9\n",
    "            i -= 1\n",
    "        if i == -1: return maxVal - minVal\n",
    "        flag = stack[i]\n",
    "        while i >= 0:\n",
    "            if stack[i] == flag:    maxVal = maxVal * 10 + 9\n",
    "            else:   maxVal = maxVal * 10 + stack[i]\n",
    "            i -= 1\n",
    "        return maxVal - minVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        n = next((c for c in num if c != '9'), '9')\n",
    "        mx = int(num.replace(n, '9'))\n",
    "        mn = int(num.replace(num[0], '0'))\n",
    "        return mx - mn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        for ch in s:\n",
    "            if ch<\"9\":\n",
    "                maxnum=s.replace(ch,\"9\")\n",
    "                break\n",
    "        else:\n",
    "            maxnum=s\n",
    "        for ch in s:\n",
    "            if ch>\"0\":\n",
    "                minnum=s.replace(ch,\"0\")\n",
    "                break\n",
    "        else:\n",
    "            minnum=s\n",
    "        return int(maxnum)-int(minnum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = [ele for ele in str(num)]\n",
    "        s1 = s.copy()\n",
    "        s2 = s.copy()\n",
    "        temp = None\n",
    "        for ele in s:\n",
    "            if ele != '9':\n",
    "                temp = ele\n",
    "                break\n",
    "        for i in range(len(s)):\n",
    "            if temp:\n",
    "                if s1[i] == temp:\n",
    "                    s1[i] = '9'\n",
    "            if s2[i] == s[0]:\n",
    "                s2[i] = '0'\n",
    "        c1 = ''\n",
    "        c2 = ''\n",
    "        for ele in s1:\n",
    "            c1 += ele\n",
    "        for ele in s2:\n",
    "            c2 += ele\n",
    "\n",
    "        return int(c1) - int(c2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def minMaxDifference(self, num: int) -> int:\r\n",
    "        condition = list()\r\n",
    "        while num // 10 != 0:\r\n",
    "            condition.append(str(num % 10))\r\n",
    "            num = num // 10\r\n",
    "        condition.append(str(num))\r\n",
    "        condition = condition[::-1]\r\n",
    "        result = ''.join(condition)\r\n",
    "        max_value = result\r\n",
    "        for i in range(len(condition)):\r\n",
    "            if condition[i] != '9':\r\n",
    "                max_value = result.replace(result[i],'9')\r\n",
    "                break\r\n",
    "        \r\n",
    "        min_value = result.replace(result[0],'0')\r\n",
    "        return int(max_value) - int(min_value)\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num=str(num)\n",
    "        num1=num\n",
    "        a=len(num)\n",
    "        for i in range(a):\n",
    "            if num[i]!='9':\n",
    "                tem=num[i]\n",
    "                num=num.replace(num[i],'9')\n",
    "                break\n",
    "        num=int(num)\n",
    "        num1=num1.replace(num1[0],'0')\n",
    "        num1=int(num1)\n",
    "        return num-num1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        # 29000\n",
    "        # 123456789\n",
    "        for x in str(num):\n",
    "            if x != '9':\n",
    "                break\n",
    "        else:\n",
    "            t_min = 0\n",
    "\n",
    "        t_max = int(str(num).replace(x, '9'))\n",
    "        for c in str(num):\n",
    "            if c != '0':\n",
    "                break\n",
    "        else:\n",
    "            t_max = 0\n",
    "\n",
    "        t_min = int(str(num).replace(c, '0'))\n",
    "\n",
    "        return t_max-t_min\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        n = len(s)\n",
    "        a, b = 0, 0\n",
    "        for i in range(n):\n",
    "            if s[i] != \"9\":\n",
    "                a = i\n",
    "                break\n",
    "        return int(s.replace(s[a], \"9\")) - int(s.replace(s[0], \"0\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        maxs = mins = s\n",
    "        for c in s:\n",
    "            if(c!='9'):\n",
    "                maxs = s.replace(c,'9')\n",
    "                break\n",
    "        for c in s:\n",
    "            if(c!='0'):\n",
    "                mins = s.replace(c,'0')\n",
    "                break\n",
    "        return int(maxs)-int(mins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def minMaxDifference(self, num: int) -> int:\n",
    "#         num=[int(d) for d in list(str(num))]\n",
    "#         mx,mi=max(num),min(num)\n",
    "#         d1=num[:]\n",
    "#         for i,d in enumerate(d1):\n",
    "#             if d==mx:\n",
    "#                 d1[i]=0\n",
    "#         d2=num[:]\n",
    "#         for i,d in enumerate(d2):\n",
    "#             if d==mi:\n",
    "#                 d2[i]=9 \n",
    "#         return int(\"\".join(str(d) for d in d2))-int(\"\".join(str(d) for d in d1))\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num):\n",
    "        num = [int(d) for d in list(str(num))]\n",
    "        d1 = num[:]\n",
    "        m =-1 # ''\n",
    "        for i, d in enumerate(d1):\n",
    "            if m<0 and d != 9:\n",
    "                m = d1[i]\n",
    "                d1[i] = 9\n",
    "            elif d == m:\n",
    "                d1[i] = 9\n",
    "        m = -1 #''\n",
    "        d2 = num[:]\n",
    "        for i, d in enumerate(d2):\n",
    "            # if not m and d != 0:\n",
    "            if m<0 and d!=0:\n",
    "                m = d2[i]\n",
    "                d2[i] = 0\n",
    "            elif d == m:\n",
    "                d2[i] = 0\n",
    "\n",
    "        return int(\"\".join(str(d) for d in d1)) - int(\"\".join(str(d) for d in d2))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        szm = '9'\n",
    "        for i in range(0, len(s)):\n",
    "            if s[i] != '9':\n",
    "                szm = s[i]\n",
    "                break\n",
    "        return int(s.replace(szm, '9')) - int(s.replace(s[0], '0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "\n",
    "        s = str(num)\n",
    "\n",
    "        for c in s:\n",
    "\n",
    "            if c != '9':\n",
    "\n",
    "                mx = int(s.replace(c, '9'))\n",
    "\n",
    "                break\n",
    "\n",
    "        return mx - int(s.replace(s[0], '0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        for i in s:\n",
    "            if i<'9':\n",
    "                max=s.replace(i,'9')\n",
    "                break\n",
    "            else:\n",
    "                max=s\n",
    "        for i in s:\n",
    "            if i>'0':\n",
    "                min=s.replace(i,'0')\n",
    "                break\n",
    "            else:\n",
    "                min=s\n",
    "        return int(max)-int(min)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        num1=str(num)\n",
    "        a,b,c=0,'9',0\n",
    "        for i,x in enumerate(num1):\n",
    "            if x!='9':\n",
    "                b=x\n",
    "                break\n",
    "        for i in num1:\n",
    "            if i==b:\n",
    "                a=a*10+9\n",
    "            else:\n",
    "                a=a*10+int(i)\n",
    "        for i in num1:\n",
    "            if i==num1[0]:\n",
    "                c=c*10\n",
    "            else:\n",
    "                c=c*10+int(i)\n",
    "        return a-c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx=num\n",
    "        s=str(num)\n",
    "        for c in s:\n",
    "            if c!='9':\n",
    "                mx=int(s.replace(c,'9'))\n",
    "                break\n",
    "        return mx-int(s.replace(s[0],'0'))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        c=None\n",
    "        s=str(num)\n",
    "        for i in s:\n",
    "            if i!='9':\n",
    "                c=i\n",
    "                break\n",
    "        S=list(s)\n",
    "        s=S[:]\n",
    "        M=num\n",
    "        if c!=None:\n",
    "            for i in range(len(s)):\n",
    "                if s[i]==c:\n",
    "                    s[i]='9'\n",
    "            M=int(''.join(s))\n",
    "        c=S[0]\n",
    "        for i in range(len(S)):\n",
    "            if S[i]==c:\n",
    "                S[i]='0'\n",
    "        m=int(''.join(S))\n",
    "        return M-m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        ori = str(num)\n",
    "        mx = ori\n",
    "        for c in ori:\n",
    "            if c != '9':\n",
    "                mx = ori.replace(c, '9')\n",
    "                break\n",
    "        mn = ori.replace(ori[0], '0')\n",
    "        return int(mx)-int(mn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        st=str(num)\n",
    "        mx,mn,k=0,0,0\n",
    "        if st[0]=='9':\n",
    "            # 最高位是 9，就找第一个不是 9 的数进行替换\n",
    "            for i in range(1,len(st)):\n",
    "                if st[i]!='9':\n",
    "                    k=i\n",
    "                    break\n",
    "            mx=st.replace(st[k],'9')\n",
    "        else:\n",
    "          # 最高位不是 9，那就替换成 9\n",
    "            mx=st.replace(st[0],'9')\n",
    "        mn=st.replace(st[0],'0')\n",
    "        diff=int(mx)-int(mn)\n",
    "        return diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        nums = str(num)\n",
    "        for c in nums:\n",
    "            if c!='9':\n",
    "                mx = int(nums.replace(c,'9'))\n",
    "                break\n",
    "        return mx-int(nums.replace(nums[0],'0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        digits = list(str(num))\n",
    "\n",
    "        p = 0\n",
    "        while p < len(digits) and digits[p] == '9':\n",
    "            p += 1\n",
    "        \n",
    "        if p < len(digits):\n",
    "            target = digits[p]       \n",
    "            ma = int(str(num).replace(target, '9'))\n",
    "        else:\n",
    "            ma = num\n",
    "\n",
    "        p = 0\n",
    "        while p < len(digits) and digits[p] == '0':\n",
    "            p += 1\n",
    "        \n",
    "        if p < len(digits):\n",
    "            target = digits[p]\n",
    "            mi = int(str(num).replace(target, '0'))\n",
    "        else:\n",
    "            mi = num\n",
    "        \n",
    "        return ma - mi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        mi = int(s.replace(s[0], \"0\"))\n",
    "        for i in range(len(s)):\n",
    "            if s[i] != \"9\":\n",
    "                return int(s.replace(s[i], \"9\")) - mi\n",
    "        return num - mi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        strNum = str(num)\n",
    "        maxNum = '9'\n",
    "        minNum = '0'\n",
    "\n",
    "        def GetChangeNum(tarNum):\n",
    "            changeNum = tarNum\n",
    "            for c in strNum:\n",
    "                if c != tarNum:\n",
    "                    changeNum = c\n",
    "                    break\n",
    "            return changeNum\n",
    "\n",
    "        def GetNum(sNum, tarNum):\n",
    "            changeNum = GetChangeNum(tarNum)\n",
    "            sNum = sNum.replace(changeNum, tarNum)\n",
    "            return int(sNum)\n",
    "\n",
    "        return GetNum(strNum, maxNum) - GetNum(strNum, minNum)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        ss=\"\"\n",
    "        sig=0\n",
    "        c=''\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='9':\n",
    "                ss+=s[i]\n",
    "                continue\n",
    "            elif s[i]!='9' and sig==0:\n",
    "                c=s[i]\n",
    "                ss+='9'\n",
    "                sig=1\n",
    "                continue\n",
    "            elif s[i]==c:\n",
    "                ss+='9'\n",
    "                continue\n",
    "            else:\n",
    "                ss+=s[i]\n",
    "        maxx=int(ss)\n",
    "        #print(maxx)\n",
    "        sss=\"\"\n",
    "        sigg=0\n",
    "        cc=''\n",
    "        for i in range(len(s)):\n",
    "            if sigg==0:\n",
    "                sss+=\"0\"\n",
    "                sigg=1\n",
    "                cc=s[i]\n",
    "                continue\n",
    "            elif s[i]==cc:\n",
    "                sss+='0'\n",
    "                continue\n",
    "            else:\n",
    "                sss+=s[i]\n",
    "        minn=int(sss)\n",
    "        print(maxx)\n",
    "        print(minn)\n",
    "        return maxx-minn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        mx = num\n",
    "        s = str(num)\n",
    "        for c in s:\n",
    "            if c != '9':\n",
    "                mx = int(s.replace(c, '9'))\n",
    "                break\n",
    "        return mx - int(s.replace(s[0], '0'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minMaxDifference(self, num: int) -> int:\n",
    "        max_num = min_num = num\n",
    "        for i in range(10):\n",
    "            max_num = max(int(str(num).replace(str(i), '9')), max_num)\n",
    "            min_num = min(int(str(num).replace(str(i), '0')), min_num)\n",
    "        return max_num - min_num"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
